package com.wangy.service.impl;

import com.wangy.bean.User;
import com.wangy.mapper.UserMapper;
import com.wangy.mapper.UserRoleMapper;
import com.wangy.service.UserService;
import com.wangy.util.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * *@Author yyzb
 * *@Date 2024/10/22 17:27
 * *@Version 1.0
 */

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public User getUserByName(String username) {
//        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("username", username);
//        List<User> userList = userMapper.selectList(queryWrapper);
        List<User> userList = userMapper.getUserByName(username);
        return userList.get(0);
    }

    @Override
    public List<User> getAllUsers() {
        List<User> userList = userMapper.getAllUsers();
        return userList;
    }

    @Override
    public User getUserById(Long id) {
        List<User> userList = userMapper.getUserById(id);
        return userList.get(0);
    }

    @Override
    public void addUser(User user) {
        User user1 = new User();
        user1.setUsername(user.getUsername());
        user1.setPassword(MD5.encrypt(user.getPassword()));
//        BeanUtils.copyProperties(user,user1);  // 密码没有MD5加密
        userMapper.addUser(user1);
    }

    @Override
    public void deleteById(Long id) {
        userMapper.deleteById(id);
        userRoleMapper.deleteByUserId(id);
    }
}
